package com.example.weatherforecast.citypagetool;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherforecast.R;
import com.example.weatherforecast.weatherdatatool.RecentWeatherInfo;

import java.util.List;

/**
 * @Author：coderChen
 * @Package：com.example.weatherforecast.citytool
 * @Project：WeatherForecast
 * @Date：2024/1/2 0:42
 * @Filename：WeatherAdapter
 */
public class RecentWeatherAdapter extends RecyclerView.Adapter<RecentWeatherAdapter.ViewHolder> {

    private List<RecentWeatherInfo> forecasts;

    public RecentWeatherAdapter(List<RecentWeatherInfo> forecasts) {
        this.forecasts = forecasts;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weather_forecast, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RecentWeatherInfo forecast = forecasts.get(position);
        holder.dateTextView.setText(forecast.getDate());
        holder.weekTextView.setText(forecast.getWeek());
        holder.weatherTypeTextView.setText(forecast.getWeatherType());
        holder.temperatureTextView.setText(forecast.getTemperature());
        // 根据当前天气类型设置不同的图片资源
        int weatherImageResource = forecast.getWeatherTypeImageResource();
        holder.weatherImageView.setImageResource(weatherImageResource);
        // 设置图片大小
        holder.weatherImageView.setLayoutParams(new LinearLayout.LayoutParams(140, 140)); // 设置图片宽高为 100dp
    }

    @Override
    public int getItemCount() {
        return forecasts.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView dateTextView;
        public TextView weekTextView;
        public TextView weatherTypeTextView;
        public TextView temperatureTextView;
        public ImageView weatherImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            dateTextView = itemView.findViewById(R.id.tvDate);
            weekTextView = itemView.findViewById(R.id.tvWeek);
            weatherTypeTextView = itemView.findViewById(R.id.tvWeather);
            temperatureTextView = itemView.findViewById(R.id.tvTemperature);
            weatherImageView = itemView.findViewById(R.id.weatherImageView);
        }
    }

}

